package com.example.administrator.base;

/**
 * Created by Administrator on 2017/12/25/025.
 */

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.Toast;
import butterknife.ButterKnife;


public abstract class BaseActivity extends AppCompatActivity {

    private ProgressDialog progressDialog;

    protected ActivityManager activityManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(getLayoutResourceId());

        ButterKnife.bind(this);

        init();
        initView();
        registerEvents();

//将activity加入堆栈
        activityManager = ActivityManager.getInstance();
        activityManager.addActivity(this);
    }

    protected abstract int getLayoutResourceId();

    /**
     * 数据初始化
     */
    protected void init(){}

    /**
     * 界面初始化
     */
    protected void initView(){}

    /**
     * 注册监听事件
     */
    protected void registerEvents(){}

    //Util Method
    public void toast(String msg){
        Toast.makeText(this,msg, Toast.LENGTH_SHORT).show();
    }

    public void showLoadBar(String message){
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(message);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }
    public void hideLoadBar(){
        progressDialog.cancel();
    }
    /**
     * 页面转跳函数，不结束前一页面
     * @param classObj
     */
    public void overlay (Class<?> classObj) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setClass(this, classObj);
        startActivity(intent);
    }

    /**
     * 页面转跳函数，不结束前一页面
     * @param classObj
     * @param params 传递数据
     */
    public void overlay (Class<?> classObj, Bundle params) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setClass(this, classObj);
        intent.putExtras(params);
        startActivity(intent);
    }

    /**
     * 页面转跳函数，结束前一页面
     * @param classObj
     */
    public void forward (Class<?> classObj) {
        Intent intent = new Intent();
        intent.setClass(this, classObj);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        activityManager.finishActivity(this);
    }

    /**
     * 页面转跳函数，结束前一页面
     * @param classObj
     * @param params 传递的数据
     */
    public void forward (Class<?> classObj, Bundle params) {
        Intent intent = new Intent();
        intent.setClass(this, classObj);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtras(params);
        startActivity(intent);
        activityManager.finishActivity(this);
    }

    public void doFinish(){
        activityManager.finishActivity(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            doFinish();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
